package com.wanter.common.login.interceptor;

import com.wanter.common.login.validate.TokenHelper;
import com.wanter.common.utils.json.JsonUtil;
import com.wanter.common.utils.result.ReturnMap;
import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 登录拦截器
 * 这里试着加了一点的token令牌机制
 * 来防止登录的时候出现别的问题
 * Created by er on 2017/7/10.
 */
public class LoginInterceptor implements HandlerInterceptor {

    private final Logger logger = Logger.getLogger(this.getClass());
    //设置不检查拦截的url地址
    private List<String> uncheckUrls;

    /**
     * 拦截器拦截前的处理方法
     *
     * @param httpServletRequest  请求
     * @param httpServletResponse 响应
     * @param o
     * @return 是否成功
     * @throws Exception 未知异常
     */
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String requestURL = httpServletRequest.getRequestURL().toString();
        if (uncheckUrls != null && uncheckUrls.size() > 0) {
            for (String url : uncheckUrls) {
                if (requestURL.equals(url)) {
                    return true;
                }
            }

        }
        ReturnMap returnMap = new ReturnMap();
        String json = null;
        String tokenId = httpServletRequest.getParameter("tokenId");
        if (StringUtils.isEmpty(tokenId)) {
            returnMap.setMessage("userId字段未知或为空");
            json = JsonUtil.objectToJson(returnMap);
        }
        if (!TokenHelper.isExitTokenId(tokenId)) {
            returnMap.setMessage("请登录当前系统");
            json = JsonUtil.objectToJson(returnMap);
        }
        if (!StringUtils.isEmpty(json)) {
            httpServletResponse.setCharacterEncoding("UTF-8");
            httpServletResponse.setContentType("application/json; charset=utf-8");
            httpServletResponse.getWriter().print(json);
            logger.info("拦截到未知用户登录");
            return false;
        }
        return true;
    }

    /**
     * 请求处理时候的执行方法
     *
     * @param httpServletRequest  请求
     * @param httpServletResponse 响应
     * @param o
     * @param modelAndView
     * @throws Exception
     */

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        logger.info("在执行拦截器的时候可以做的一些的日志处理 可以在这里来实现的噶");
    }


    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        String tokenId = httpServletRequest.getParameter("tokenId");
        if(!StringUtils.isEmpty(tokenId)){
            TokenHelper.updateTokenBytokenId(tokenId);
            logger.info("当前的tokenId被重新更新过 ");
        }
    }


    public List<String> getUncheckUrls() {
        return uncheckUrls;
    }

    public void setUncheckUrls(List<String> uncheckUrls) {
        this.uncheckUrls = uncheckUrls;
    }
}
